<!-- Web Download (Search Result) PHP File -->
<!-- Reveions History ...
	Rev 1.00 at 2015/02/23 by Max Yin:
		1. First time create
	Rev 1.01 at 2015/07/08 by Max Yin:
		1. 由標楷體, 改成新細明體
		2. 修正 Router, WS-PROFILE, Document, 点下 [Submit] 键后, 出现 $get_sn, $get_pn error!
	Rev 1.03 at 2015/07/29
		1. ICT 新增加 P/N 填入, 因有时没有 S/N by 雪群要求
		2. ICT 能够 by S/N 或 P/N 查询
	Rev 1.06 at 2015/11/26
		1. 解决新流程卡内, 若有字串为 'QC','P/K','STOCK','Ship'的为 F/T 站之后, 皆列出 15 个栏位, 以供 RMA 查询用。
	Rev 1.07 at 2015/11/30 by Max Yin,
		1. 解决 Web 上传系统, 若有重复的 P/N, 站别, 版本名没有拦截下来, 导致重复建立站别。(PPE-Shaoxin_huang@JOYTECH)
		2. 解决 Web 下载系统, select name='webdl_select_wsid', 设定错误写成 select id='webdl_select_wsid'的问题。(SMT-eric_liang@joytech )
	REV 1.08 AT 20190823
		1.add TSlabel 
-->
<html>
	<body background="pic/background_06.gif">
	<head>
		<font face="新細明體,time new roman,Arial">
		<meta charset="UTF-8">
	</head>
	<body>
		<center>
			<font size="5" color="#0000FF"><b>--- 下载程式/文件,查询结果 ---</b></font><br>
			<hr>
			<?php
				// Loading all php class variable and fucntions ...
				require_once('class_package/common.php');
				
				// 各下载程式, 输入资料判断 ...
				$get_prgm_select = $_POST['prgm_select'];
				$append_InputFailStr[] = '';
				$webdl_work_order = "";
				$ict_show_all = 0;
				
				switch ( $get_prgm_select ) {
					case '产测程式':
						$get_sn_temp		= strtoupper($_POST['webdl_sn']);
						$get_pn				= strtoupper($_POST['webdl_pn']);
						// $webdl_work_order	= $_POST['webdl_work_order'];
						if ($get_sn_temp == '') $append_InputFailStr[]='SN'; else $webdl_sn=$get_sn_temp;
						if ($get_pn == '') $append_InputFailStr[]='PN'; else $webdl_pn=$get_pn;
						break;
					case 'Hi-Pot程式':
						$webdl_sn			='N/A';
						$get_pn				= strtoupper($_POST['webdl_pn']);
						$webdl_wsid			='H/P';
						if ($get_pn == '') $append_InputFailStr[]='PN'; else $webdl_pn=$get_pn;
						break;
					case 'Flash烧录程式':
						$webdl_sn			='N/A';
						$get_pn				= strtoupper($_POST['webdl_pn']);
						$webdl_wsid			='FLASH';
						$webdl_work_order	= $_POST['webdl_work_order'];
						if ($get_pn == '') $append_InputFailStr[]='PN'; else $webdl_pn=$get_pn;
						break;
					case 'ICT程式':
						$get_sn_temp		= strtoupper($_POST['webdl_sn']);
						$webdl_pn			= strtoupper($_POST['webdl_pn']);
						if ( $get_sn_temp == NULL ) $webdl_sn='N/A'; else $webdl_sn=$get_sn_temp;
						if ( $get_sn_temp == NULL && $webdl_pn == NULL ) {
							echo "<script>alert('由 S/N 及 P/N 不能皆为空白, 再重新输入!!');history.go(-1);</script>";
							exit();
						}
						break;
					case '捞板程式':
						$webdl_sn			='N/A';
						$webdl_pn			='N/A';
						$webdl_wsid			='ROUTER';
						break;
					case 'WS-PROFILE程式':
						$webdl_sn			='N/A';
						$webdl_pn			='N/A';
						$webdl_wsid			='WS-PROFILE';
						break;
					case 'ESD程式':
						$webdl_sn			='N/A';
						$webdl_pn			='N/A';
						$webdl_wsid			='ESD';
						break;
					case '文件下载':
						$webdl_sn			='N/A';
						$webdl_pn			='N/A';
						$webdl_wsid			='DOC';
						if ($_POST['webdl_doc'] == '') $append_InputFailStr[]='DOC'; else $webdl_tpver=$_POST['webdl_doc'];
						break;
					case '工具程式':
						$webdl_sn			='N/A';
						$webdl_pn			='N/A';
						$webdl_wsid			='TOOL';
						if ($_POST['webdl_ver'] == '') $webdl_tpver='all'; else $webdl_tpver=$_POST['webdl_ver'];
						break;
					case 'SMT程式':
						$webdl_sn			='N/A';
						$get_pn				= strtoupper($_POST['webdl_pn']);
						$get_line			= strtoupper($_POST['webdl_line']);
						$get_panel			= strtoupper($_POST['webdl_panel']);
						if ($get_pn == '') $append_InputFailStr[]='PN'; else $webdl_pn=$get_pn;
						if ($get_line == '') $append_InputFailStr[]='line'; else $webdl_line=$get_line;
						if ($get_panel == '') $append_InputFailStr[]='panel'; else $webdl_panel=$get_panel;
						break;
					case 'CSDI':
						$webdl_sn			='N/A';
						$webdl_wsid			='REPAIR';
						$get_pn 			= strtoupper($_POST['webdl_pn']);
						if ($get_pn == '') $append_InputFailStr[]='PN'; else $webdl_pn=$get_pn;
						break;
					default:
						echo "<script>alert('查无此程式下载的选项!!');history.go(-1);</script>";
						exit();
						break;
				}
				
				// 输入查询资料各栏位中, 不能为空值
				// 输入查询资料-Part Number(局部料号), 应该为 13 字元
				// 输入错误判断, 提示讯息; php执行完alert之后, 返回上一页 ...
				if ( count($append_InputFailStr) > 1 ) {
					echo "<script>alert('输入的查询栏位中, 不能为空值, 请重新输入!!');history.go(-1);</script>";
					exit();
				}
				if ( $get_prgm_select == '产测程式' || $get_prgm_select == 'CSDI' ||$get_prgm_select == 'Hi-Pot程式' || $get_prgm_select == 'Flash烧录程式' ) {
					if ( strlen($webdl_pn) != 13 ) {
						echo "<script>alert('输入的查询栏位中-Part Number(局部料号), 应该为 13 个字元, 请重新输入!!');history.go(-1);</script>";
						exit();
					}
				}
				
				if ( $get_prgm_select == '产测程式' || ($get_prgm_select == 'ICT程式' && $webdl_sn != 'N/A' && $webdl_sn != NULL) ) {
					// SoapClient(客户端) send S/N to read SFIS WebServer P/N & Next Station...
					//把服务程序的 URL 地址传递给soapclient类的构造函数。
					if ( $_POST['webdl_server'] == 'Joytech' ) 
						$client=new SoapClient("http://sf_ws.joytech.com.cn/ShopfloorWebService/Service_AP.asmx?wsdl");		// JoyTech Web Sevice ...
					else
						$client=new SoapClient("http://sf_ws.accton.com.tw/ShopfloorWebService/Service_AP.asmx?wsdl");		// Accton Web Sevice ...
					
					$client->soap_defencoding='utf-8';
					$client->decode_utf8=false;
					$client->xml_encoding='utf-8';
					
					// Show Web Sevice using functions ...
					// echo ("SOAP服务器提供的开放函数:");
					// echo (' '); 
					// var_dump ( $client->__getFunctions () ); 
					// echo ("SOAP服务器提供的Type:");
					// echo (' '); 
					// var_dump ( $client->__getTypes () );
					
					/* 由 SoapClient(客户端) 获取 P/N, Next Station ...
						Read Show Result:
						stdClass Object ( [getSFDataResult] => 1OK 20261505270191 C-G530347J-1-1-4 C0PRH4628002A 12.00 C09 CONFIG FT1 FT1 0 1 2015-05-30T21:22:03+08:00 2015-05-28T06:55:00+08:00 N/A 0 0 0 916 C-G1B0332J-1-1-1 N/A N/A N/A 0 N/A 2038129 C0PRH4628002A N/A 0 J1433047 N v6.2r1.0X1.50 TMO_XML_DLL2.4 N/A N N/A N E01C41EEFA00 1   ) 
						Detail org code:
						stdClass Object
						(
							[getSFDataResult] => <OUTPUTSTRING><rtncode><code>1</code><msg>OK</msg></rtncode><NewDataSet>
						  <data>
							<SERIAL_NUMBER>NS1413C0212</SERIAL_NUMBER>
							<MO_NUMBER>P-R420031J-1-1-1</MO_NUMBER>
							<MODEL_NAME>3HE08158AA</MODEL_NAME>
							<VERSION_CODE>1.00</VERSION_CODE>
							<LINE_NAME>P09</LINE_NAME>
							<SECTION_NAME>PACKING</SECTION_NAME>
							<GROUP_NAME>P/K-2 CARTON</GROUP_NAME>
							<STATION_NAME>P/K-2 CARTON</STATION_NAME>
							<STATION_SEQ>0</STATION_SEQ>
							<ERROR_FLAG>0</ERROR_FLAG>
							<IN_STATION_TIME>2014-03-29T02:45:10+08:00</IN_STATION_TIME>
							<IN_LINE_TIME>2014-03-27T10:10:43+08:00</IN_LINE_TIME>
							<OUT_LINE_TIME>2014-03-29T02:45:10+08:00</OUT_LINE_TIME>
							<SHIPPING_SN>NS1413C0212</SHIPPING_SN>
							<WORK_FLAG>0</WORK_FLAG>
							<FINISH_FLAG>0</FINISH_FLAG>
							<ENC_CNT>0</ENC_CNT>
							<SPECIAL_ROUTE>10</SPECIAL_ROUTE>
							<PALLET_NO>N/A</PALLET_NO>
							<CONTAINER_NO>N/A</CONTAINER_NO>
							<QA_NO>N/A</QA_NO>
							<QA_RESULT>N/A</QA_RESULT>
							<SCRAP_FLAG>0</SCRAP_FLAG>
							<NEXT_STATION>N/A</NEXT_STATION>
							<BOM_NO>1725257</BOM_NO>
							<KEY_PART_NO>FRL615766004A</KEY_PART_NO>
							<CARTON_NO>A1403V4022</CARTON_NO>
							<REPAIR_CNT>0</REPAIR_CNT>
							<EMP_NO>J1114894</EMP_NO>
							<PALLET_FULL_FLAG>N</PALLET_FULL_FLAG>
							<MACHINE2>PK_CARTON_2.47.21.0</MACHINE2>
							<SPEC_NAME>N/A</SPEC_NAME>
							<STATUS_FLAG>Y</STATUS_FLAG>
							<BOX_NO>N/A</BOX_NO>
							<CHECK_FLAG>N</CHECK_FLAG>
							<WEIGHT_CHECK>1</WEIGHT_CHECK>
							<WEIGHT>1409</WEIGHT>
							<MAC_ID>4C5FD2F160E0</MAC_ID>
							<STATE_FLAG>0</STATE_FLAG>
						  </data>
						</NewDataSet></OUTPUTSTRING>
						)
					*/
					
					// 由 SoapClient(客户端) 获取 P/N ...
					$iFailChk  = 0;
					$sGetNewSN = '';
					// S/N 2D range...
					$sn_2d_arry[1] = "28-38";		// 61P
					$sn_2d_arry[2] = "14-23";		// 61F
					$sn_2d_arry[3] = "44-55";		// 09J
					$sn_2d_arry[4] = "7-18";		// C1,09,D8,CAM,SN,09J
					$sn_2d_arry[5] = "0-11";		// C1,D8
					$sn_2d_arry[6] = "7-15";		// 07
					$sn_2d_arry[7] = "7-16";		// CAN,ARH
					$sn_2d_arry[8] = "3-16";		// AIE
					while (1) {
						if ( $iFailChk == 0 ) {
							$sGetNewSN = $webdl_sn; 
						} else {
							if ( $iFailChk > 8 ) {
								echo "<script>alert('由 S/N 在 SFIS 获取不到 P/N, 请确认 S/N 后, 再重新输入!!');history.go(-1);</script>";
								exit();
							}
							
							// To search all table for get 2D Label S/N Range...
							$new_tmp_sn	  = $sn_2d_arry[$iFailChk];							
							$sGet2dLen    = strlen($new_tmp_sn);
							$iMiddleNum   = strpos($new_tmp_sn,'-');							// 检索'-'在字符串中首次出现的位置。
							$iSN_StartNum = substr($new_tmp_sn,0,$iMiddleNum-0);				// 获取 2D Label S/N 在资料库中定义的起始位置
							$iSN_EndNum   = substr($new_tmp_sn,$iMiddleNum+1,$sGet2dLen-1);		// 获取 2D Label S/N 在资料库中定义的结束位置
							$iSN_Rnage    = $iSN_EndNum-$iSN_StartNum;
							$sGetNewSN    = substr($webdl_sn,$iSN_StartNum,$iSN_Rnage+1);		// 获取 2D Label S/N 值
							// echo "new_tmp_sn == $new_tmp_sn<br>";
							// echo "sGet2dLen == $sGet2dLen<br>";
							// echo "iMiddleNum == $iMiddleNum<br>";
							// echo "iSN_StartNum == $iSN_StartNum<br>";
							// echo "iSN_EndNum == $iSN_EndNum<br>";
							// echo "iSN_Rnage == $iSN_Rnage<br>";
							// echo "sGetNewSN == $sGetNewSN<br>";
							// exit();
							
							// new_tmp_sn == 28-38
							// sGet2dLen == 5
							// iMiddleNum == 2
							// iSN_StartNum == 28
							// iSN_EndNum == 38
							// iSN_Rnage == 10
							// sGetNewSN == NS1449C1202
						}
						
						$param_pn=array('INPUTSTRING'=>"<INPUTSTRING><rowset><DataType>SN_Find_Wip</DataType><serial_number>$sGetNewSN</serial_number><Return_Formate>XML</Return_Formate></rowset></INPUTSTRING>");
						$result=$client->__soapCall('getSFData',array($param_pn));
						$get_sfis_pn='';
						if ( $result ) {
							$data = new xml();
							$result_all_vars = $data->__make_xmk($result);
							// $result_all_vars_org=htmlspecialchars($result_all_vars);
							// From S/N to get P/N
							// <KEY_PART_NO>FRL615766004A</KEY_PART_NO>
							$starStr = '<KEY_PART_NO>';
							$endStr = '</KEY_PART_NO>';
							$start = strpos($result_all_vars,$starStr)+strlen($starStr);	//检索<KEY_PART_NO>在字符串中首次出现的位置。
							$end = strpos($result_all_vars,$endStr);						//检索</KEY_PART_NO>在字符串中末尾出现的位置。
							$get_sfis_pn = substr($result_all_vars,$start,$end-$start);
							
							// From S/N to get Work Order
							// <MO_NUMBER>P-R420031J-1-1-1</MO_NUMBER>
							unset($get_wo_od_arry);
							$starStr = '<MO_NUMBER>';
							$endStr = '</MO_NUMBER>';
							$start = strpos($result_all_vars,$starStr)+strlen($starStr);	//检索<KEY_PART_NO>在字符串中首次出现的位置。
							$end = strpos($result_all_vars,$endStr);						//检索</KEY_PART_NO>在字符串中末尾出现的位置。
							$get_wo_od_tmp = substr($result_all_vars,$start,$end-$start);
							$get_wo_od_arry = str_split($get_wo_od_tmp);
							$webdl_work_order = $get_wo_od_arry[2];
							
							if ( $get_sfis_pn == '' || strlen($get_sfis_pn) != 13 ) {
								if ( $iFailChk < 9 ) {
									$iFailChk++;
									continue;
								} else {
									echo "<script>alert('由 S/N 在 SFIS 获取不到 P/N, 请确认 S/N 后, 再重新输入!!');history.go(-1);</script>";
									exit();
								}
							}
							
							if ( $get_prgm_select == 'ICT程式' ) {
								$webdl_pn = $get_sfis_pn;
							} else {
								if ( substr($get_sfis_pn,1,12) != substr($webdl_pn,1,12) ) {
									if ( $iFailChk < 9 ) {
										$iFailChk++;
										continue;
									} else {
										echo "<script>alert('SFIS 获取的 P/N 与输入的 P/N 不一致, 请再重新输入!!');history.go(-1);</script>";
										exit();
									}
								}
							}
						} else {
							if ( $iFailChk < 9 ) {
								$iFailChk++;
								continue;
							} else {
								echo "<script>alert('由 S/N 在 SFIS 获取不到 P/N, 请确认 S/N 后, 再重新输入!!');history.go(-1);</script>";
								exit();
							}
						}
						// echo "Part Number is: $webdl_pn<br>";
						// echo "MO_NUMBER is: $get_wo_od_tmp<br>";
						// echo "webdl_work_order is: $webdl_work_order<br>";
						
						// 由 SoapClient(客户端) 获取 Next Station ...
						$param_station=array('INPUTSTRING'=>"<INPUTSTRING><rowset><DataType>A_GET_SN_NEXT_STATION</DataType><serial_number>$sGetNewSN</serial_number><Return_Formate>XML</Return_Formate></rowset></INPUTSTRING>");
						$result=$client->__soapCall('getSFData',array($param_station));
						$get_sfis_next_station = '';
						if ( $result ) {
							$data = new xml();
							$result_all_vars = $data->__make_xmk($result);
							// $starStr = "<NEXT_STATION>REPAIR(";
							// $endStr = ")</NEXT_STATION>";
							// F/T-B1
							// REPAIR(F/T-B1)
							// FQC
							// Route End
							
							// To get part-1 ...
							$starStr = '<NEXT_STATION>';
							$endStr = '</NEXT_STATION>';
							$start = strpos($result_all_vars,$starStr)+strlen($starStr);	//检索 <NEXT_STATION>在字符串中首次出现的位置。
							$end = strpos($result_all_vars,$endStr);						//检索 </NEXT_STATION> 在字符串中末尾出现的位置。
							$get_sfis_next_station1 = substr($result_all_vars,$start,$end-$start);
							
							// To get part-2 ...
							$get_sfis_next_station2 = '';
							$starStr = 'REPAIR(';
							$endStr = ')';
							$start = strpos($get_sfis_next_station1,$starStr)+strlen($starStr);	//检索 REPAIR( 在字符串中首次出现的位置。
							$end = strpos($get_sfis_next_station1,$endStr);						//检索 ) 在字符串中末尾出现的位置。
							$get_sfis_next_station2 = substr($get_sfis_next_station1,$start,$end-$start);
							
							if ( $get_sfis_next_station2 == '' )
								$get_sfis_next_station = $get_sfis_next_station1;
							else 
								$get_sfis_next_station = $get_sfis_next_station2;
								
							if ( $get_sfis_next_station == '' ) {
								echo "<script>alert('由 S/N 获取不到\'下一站\'资讯, 请确认 S/N 后, 再重新输入!!');history.go(-1);</script>";
								exit();
							}
							$webdl_wsid = $get_sfis_next_station;
						} else {
							echo "<script>alert('由 S/N 获取\'下一站\'资讯错误错误, 请确认 S/N 后, 再重新输入!!');history.go(-1);</script>";
							exit();
						}
						$webdl_sn = $sGetNewSN;
						// echo "webdl_wsid is: $webdl_wsid<br>";
						// exit();
						break;
					}
				}
				
				// To search all table ...
				$db_select   = Sql_Download::sql_db;
				$db_table    = Sql_Download::sql_table;
				$table_name1 = Sql_Download::table_name1;	//webdl_pn
				$table_name2 = Sql_Download::table_name2;	//webdl_wsid
				$table_name3 = Sql_Download::table_name3;	//webdl_tpver
				$table_name4 = Sql_Download::table_name4;	//webdl_work_order
				$table_name5 = Sql_Download::table_name5;	//webdl_line
				$table_name6 = Sql_Download::table_name6;	//webdl_panel
				$conn = '';
				$sql_cmd = '';
				$rows = 0;
				$result = '';
				$prg_rows_fail = 0;
				$sop_rows_fail = 0;
				
				// Check connection for show program...
				$conn = mysqli_connect(html::sql_login_host,html::sql_login_user,html::sql_login_pass,$db_select);
				if ( mysqli_connect_errno($conn) ) { 
					echo "<script>alert('MySql资料库连接失败,请联系开发人员,并查明原因...');history.go(-1);</script>";
					exit();
				}
				mysqli_set_charset($conn,'set names utf8');
			
				// 要是字段是字符型，必须要用''（单引号）括起来，要是数值型，若中间有空格，可以用''（单引号）括起来; 若没有空格，也可以不用...
				// SELECT *  FROM `download_data` WHERE `pn` = 'CRL615766000A' AND `wsid` LIKE 'F/T-B%' AND `tpver` = '0X1.16'
				switch ( $get_prgm_select ) {
					case '产测程式':
						// 将字符的数字转成数字，比如'0'转成0可以直接用加法来实现
						// 例如：将pony表中的d 进行排序，可d的定义为varchar，可以这样解决
						// select * from pony order by (d+0)
						// select server_id from cardserver where game_id = 1 order by CONVERT(server_id,SIGNED) desc limit 10;
						// order by 字段名 ASC limit 0,1.最小
						// order by 字段名 DESC limit 0,1.最大
						if ( $webdl_wsid == 'Route End' || strpos($webdl_wsid,'QC') > -1 || strpos($webdl_wsid,'P/K') > -1 || strpos($webdl_wsid,'STOCK') > -1 || strpos($webdl_wsid,'Ship') > -1 || $webdl_work_order == "K" || $webdl_work_order == "M" || $webdl_work_order == "L" ) {
							$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name1`='$webdl_pn' AND `$table_name4`='$webdl_work_order' ORDER BY (`$table_name3`) DESC LIMIT 0,15";
						} else {
							$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name1`='$webdl_pn' AND `$table_name2`='$webdl_wsid' AND `$table_name4`='$webdl_work_order' ORDER BY (`$table_name3`) DESC LIMIT 0,1";
						}
						break;
					case 'Hi-Pot程式':
						$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name1`='$webdl_pn' AND `$table_name2`='$webdl_wsid' ORDER BY (`$table_name3`) DESC LIMIT 0,1";
						break;
					case 'Flash烧录程式':
						$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name1`='$webdl_pn' AND `$table_name2`='$webdl_wsid' AND `$table_name4`='$webdl_work_order' ORDER BY (`$table_name3`) DESC LIMIT 0,1";	// 从大到小排序
						// $sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name1`='$webdl_pn' AND `$table_name2`='$webdl_wsid' AND `$table_name4`='$webdl_work_order' ORDER BY (`$table_name3`) ASC LIMIT 0,1";	// 从小到大排序
						break;
					case 'ICT程式':
						if ( $webdl_sn == 'N/A' || $webdl_sn == NULL ) {
							$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name1`='$webdl_pn' ORDER BY (`$table_name3`) DESC LIMIT 0,15";
							$webdl_wsid = 'N/A';
							$ict_show_all = 1;
						} else {						
							if ( $webdl_wsid == "Route End" || strpos($webdl_wsid,"QC") > -1 || strpos($webdl_wsid,"P/K") > -1 || strpos($webdl_wsid,"STOCK") > -1 || strpos($webdl_wsid,"Ship") > -1 ) {
								$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name1`='$webdl_pn' ORDER BY (`$table_name3`) DESC LIMIT 0,15";
								$ict_show_all = 1;
							} else {
								$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name1`='$webdl_pn' AND `$table_name2` LIKE '$webdl_wsid%' ORDER BY (`$table_name3`) DESC LIMIT 0,7";
							}
						}
						break;
					case '捞板程式':
						$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name2`='$webdl_wsid' ORDER BY (`$table_name3`) DESC LIMIT 0,1";
						break;
					case 'WS-PROFILE程式':
						$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name2`='$webdl_wsid' ORDER BY (`$table_name3`) DESC LIMIT 0,1";
						break;
					case 'ESD程式':
						$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name2`='$webdl_wsid' ORDER BY (`$table_name3`) DESC LIMIT 0,1";
						break;
					case '文件下载':
						if ( strtolower($webdl_tpver) == 'all' )
							$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name2`='$webdl_wsid' ORDER BY (`$table_name3`) DESC";
						else 
							$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name2`='$webdl_wsid' AND `$table_name3` LIKE '%$webdl_tpver%'";
						break;
					case '工具程式':
						if ( strtolower($webdl_tpver) == 'all' )
							$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name2`='$webdl_wsid' ORDER BY (`$table_name3`) DESC";
						else 
							$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name2`='$webdl_wsid' AND `$table_name3` LIKE '%$webdl_tpver%'";
						break;
						
					case 'SMT程式':
						$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name1`='$webdl_pn' AND `$table_name5`='$webdl_line' AND `$table_name6`='$webdl_panel' ORDER BY (`$table_name3`) DESC LIMIT 0,1";
						break;
					case 'CSDI':
						$sql_cmd = "SELECT *FROM `$db_table` WHERE `$table_name1`='$webdl_pn' AND `$table_name2`='$webdl_wsid' ";
						break;
					default:
						echo "<script>alert('查无此程式下载的选项!!');history.go(-1);</script>";
						exit();
						break;
				}
				
				$result = mysqli_query($conn,$sql_cmd);	// 透过 execute_query 对象实例完成查询
				if ( !$result ) { 
					mysqli_close($conn);				// 关闭资料库
					echo "<script>alert('MySql资料库搜寻失败, 请跟开发人员查明原因...');history.go(-1);</script>";
					exit();
				}
				
				$rows = mysqli_num_rows($result);		// 返回结果集中栏位的数量。
				if ( $rows == 0 ) { 
					$prg_rows_fail = 1;
				} else {
					switch ( $get_prgm_select ) {
						case '产测程式':
						case 'Flash烧录程式':
							if ( $webdl_wsid == "Route End" || strpos($webdl_wsid,"QC") > -1 || strpos($webdl_wsid,"P/K") > -1 || strpos($webdl_wsid,"STOCK") > -1 || strpos($webdl_wsid,"Ship") > -1 || $webdl_work_order == "K" || $webdl_work_order == "M" || $webdl_work_order == "L" ) {
								echo "<br><font size=\"4\" color=\"#FF0000\"><b>(Note: DUT已经进入[F/T]站之后或K,M,L工单,只列出15笔以内资料,供维修及确认用。)</b></font><br>";
							}
							
							echo '<br>
							<font size="4" color="#000000"><b>1.MFG(測試程式):</b></font>
							<table border="2" width="75%">
								<tr bgcolor="#70DB93">
									<th>No.</th>
									<th>工單</th>
									<th>工单号</th>
									<th>Serial Number</th>
									<th>Part Number</th>
									<th>WSID</th>
									<th>TPVER</th>
									<th>线别</th>
									<th>制程别</th>
									<th>Create Date\Time</th>
									<th>Create By</th>
									<th colspan="2">Description(位址\ChkSum)</th>	
									<th>TSLabel(烧录贴纸样板)</th>									
									<th>Download</th>
								</tr>';
							break;
							
						case 'Hi-Pot程式':
						case 'ICT程式':
						case '捞板程式':
						case 'WS-PROFILE程式':
						case 'ESD程式':
						case 'SMT程式':
							if ( $ict_show_all == 1 ) {
								echo '<br><font size="4" color="#000000"><b>1.MFG(測試程式)/SOP(制程规范):</b></font>';
							} else {
								echo '<br><font size="4" color="#000000"><b>1.MFG(測試程式):</b></font>';
							}
							
							echo '<table border="2" width="75%">
								<tr bgcolor="#70DB93">
									<th>No.</th>
									<th>工單</th>
									<th>Serial Number</th>
									<th>Part Number</th>
									<th>WSID</th>
									<th>TPVER</th>
									<th>线别</th>
									<th>制程别</th>
									<th>Create Date/Time</th>
									<th>Create By</th>
									<th>Description</th>
									<th>Download</th>
								</tr>';
							break;
						case '文件下载':
							echo '<br>
							<table border="2" width="75%">
								<tr bgcolor="#70DB93">
									<th>No.</th>
									<th>工單</th>
									<th>Serial Number</th>
									<th>Part Number</th>
									<th>WSID</th>
									<th>TPVER</th>
									<th>线别</th>
									<th>制程别</th>
									<th>Create Date/Time</th>
									<th>Create By</th>
									<th>Description</th>
									<th>Download</th>
								</tr>';
							break;
						case '工具程式':
							echo '<br>
							<table border="2" width="75%">
								<tr bgcolor="#70DB93">
									<th>No.</th>
									<th>工單</th>
									<th>Serial Number</th>
									<th>Part Number</th>
									<th>WSID</th>
									<th>TPVER</th>
									<th>线别</th>
									<th>制程别</th>
									<th>Create Date/Time</th>
									<th>Create By</th>
									<th>Description</th>
									<th>Download</th>
								</tr>';
							break;
						case 'CSDI':
							echo '<br><font size="4" color="#000000"><b>CSDI程式</b></font>';
							echo '<br>
							<table border="2" width="75%">
								<tr bgcolor="#70DB93">
									<th>No.</th>
									<th>工單</th>
									<th>Serial Number</th>
									<th>Part Number</th>
									<th>WSID</th>
									<th>TPVER</th>
									<th>线别</th>
									<th>制程别</th>
									<th>Create Date/Time</th>
									<th>Create By</th>
									<th>Description</th>
									<th>Download</th>
								</tr>';
							break;
						default:
							echo "<script>alert('查无此程式下载的选项!!');history.go(-1);</script>";
							exit();
							break;
					}
					$show_num = 0;
					while ( $getArray = mysqli_fetch_array($result,MYSQLI_ASSOC) ) {
						// To loading all datas from MySQL ...
						$show_num++;
						$put_pn          = $getArray['webdl_pn'];
						$put_wsid        = $getArray['webdl_wsid'];
						$put_tpver       = $getArray['webdl_tpver'];
						$put_work_order  = $getArray['webdl_work_order'];
						$put_line        = $getArray['webdl_line'];
						$put_panel       = $getArray['webdl_panel'];
						$put_create_date = $getArray['webdl_create_date'];
						$put_creator     = $getArray['webdl_creator'];
						$put_description = $getArray['webdl_description'];
						$put_ftpip       = $getArray['webdl_ftpip'];
						$put_count       = $getArray['webdl_count'];
						$put_description = $getArray['webdl_description'];
						$put_description_arry  = explode("/",$put_description);
						$download_ftpip  = "webdl_download_count.php?pn=$put_pn&wsid=$put_wsid&tpver=$put_tpver&work_order=$put_work_order&line=$put_line&panel=$put_panel&ftpip=$put_ftpip&webdl_count=$put_count&creator=$put_creator";
						// $download_ftpip  = "webdl_download_count.php?pn=$put_pn&wsid=$put_wsid&tpver=$put_tpver&work_order=$put_work_order&line=$put_line&panel=$put_panel&ftpip=$put_ftpip&webdl_count=$put_count";
						if($get_prgm_select =="Flash烧录程式"){
							$put_work_order_number   	=$getArray['webdl_order_number'];
							$put_TSLabel     			=$getArray['webdl_TSLabel'];
							
							
							if(empty( $put_work_order_number)){
								$put_work_order_number="N/A";
							}
							if(empty( $put_TSLabel)){
								$put_TSLabel="N/A";
							}else{
								$put_TSLabel_arry  = explode("/",$put_TSLabel);
								$put_TSLabel=$put_TSLabel_arry[0].'<br>'.$put_TSLabel_arry[1];
							}
							
				
							
							if(count($put_description_arry)<2){
								$put_description_arry[1]="N/A";
								
								echo "
								<tr>
									<th>$show_num</th>
									<th><font size='4' color='#0000FF'><b>$put_work_order</b></font></th>
									<th>$put_work_order_number</th>
									<th>$webdl_sn</th>
									<th>$put_pn</th>
									<th>$put_wsid</th>
									<th>$put_tpver</th>
									<th>$put_line</th>
									<th>$put_panel</th>
									<th>$put_create_date</th>
									<th>$put_creator</th>
									<th>$put_description_arry[0]</th>									
									<th>$put_description_arry[1]</th>	
									<th>$put_TSLabel</th>
									<th><a href=$download_ftpip >下 载</a></th>					
								</tr>";
							}else {
								echo "
								<tr>
									<th>$show_num </th>
									<th><font size='4' color='#0000FF'><b>$put_work_order</b></font></th>
									<th>$put_work_order_number</th>
									<th>$webdl_sn</th>
									<th>$put_pn</th>
									<th>$put_wsid</th>
									<th>$put_tpver</th>
									<th>$put_line</th>
									<th>$put_panel</th>
									<th>$put_create_date</th>
									<th>$put_creator</th>									
									<th>$put_description_arry[0]</th>
									<th>$put_description_arry[1]</th>	
									<th>$put_TSLabel</th>
									<th><a href=$download_ftpip >下 载</a></th>					
								</tr>";
							}
						}else {
							echo "
							<tr>
								<th>$show_num</th>
								<th><font size='4' color='#0000FF'><b>$put_work_order</b></font></th>
								<th>$webdl_sn</th>
								<th>$put_pn</th>
								<th>$put_wsid</th>
								<th>$put_tpver</th>
								<th>$put_line</th>
								<th>$put_panel</th>
								<th>$put_create_date</th>
								<th>$put_creator</th>
								<th>$put_description</th>							
								<th><a href=$download_ftpip>下 载</a></th>					
							</tr>";
						}				
						
						//$download_ftpip  = "webdl_download_count.php?pn=$put_pn&wsid=$put_wsid&tpver=$put_tpver&work_order=$put_work_order&line=$put_line&panel=$put_panel&ftpip=$put_ftpip&webdl_count=$put_count";
						
					
					}
					echo '</table><br>';
				}
				mysqli_free_result($result);			// 释放 MySQL $result
				mysqli_close($conn);					// 关闭资料库
				
				// Check connection for show SOP...
				if ( $ict_show_all == 0 && $get_prgm_select != 'SMT程式' && $get_prgm_select != 'CSDI' && $get_prgm_select != '工具程式' && $get_prgm_select != '文件下载' && $webdl_wsid != 'Route End' && strpos($webdl_wsid,'QC') == NULL && strpos($webdl_wsid,'P/K') == NULL && strpos($webdl_wsid,'STOCK') == NULL && strpos($webdl_wsid,'Ship') == NULL && $webdl_work_order != "K" && $webdl_work_order != "M" && $webdl_work_order != "L" ) {
					$conn = mysqli_connect(html::sql_login_host,html::sql_login_user,html::sql_login_pass,$db_select);
					if ( mysqli_connect_errno($conn) ) { 
						echo "<script>alert('MySql资料库连接失败,请联系开发人员,并查明原因...');history.go(-1);</script>";
						exit();
					}
					mysqli_set_charset($conn,'set names utf8');
				
					// 要是字段是字符型，必须要用''（单引号）括起来，要是数值型，若中间有空格，可以用''（单引号）括起来; 若没有空格，也可以不用...
					// SELECT *  FROM `download_data` WHERE `pn` = 'CRL615766000A' AND `wsid` LIKE 'F/T-B%' AND `tpver` = '0X1.16'
					$webdl_wsid = sprintf("%s_SOP",$webdl_wsid);
					switch ( $get_prgm_select ) {
						case '产测程式':
							// 将字符的数字转成数字，比如'0'转成0可以直接用加法来实现
							// 例如：将pony表中的d 进行排序，可d的定义为varchar，可以这样解决
							// select * from pony order by (d+0)
							// select server_id from cardserver where game_id = 1 order by CONVERT(server_id,SIGNED) desc limit 10;
							// order by 字段名 ASC limit 0,1.最小
							// order by 字段名 DESC limit 0,1.最大
							$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name1`='$webdl_pn' AND `$table_name2`='$webdl_wsid' AND `$table_name4`='$webdl_work_order' ORDER BY (`$table_name3`) DESC LIMIT 0,1";
							break;
						case 'Hi-Pot程式':
							$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name1`='$webdl_pn' AND `$table_name2`='$webdl_wsid' ORDER BY (`$table_name3`) DESC LIMIT 0,1";
							break;
						case 'Flash烧录程式':
							$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name1`='$webdl_pn' AND `$table_name2`='$webdl_wsid' AND `$table_name4`='$webdl_work_order' ORDER BY (`$table_name3`) DESC LIMIT 0,1";
							break;
						case 'ICT程式':
							$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name1`='$webdl_pn' AND `$table_name2` LIKE '$webdl_wsid%' ORDER BY (`$table_name3`) DESC LIMIT 0,7";
							break;
						case '捞板程式':
							$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name2`='$webdl_wsid' ORDER BY (`$table_name3`) DESC LIMIT 0,1";
							break;
						case 'WS-PROFILE程式':
							$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name2`='$webdl_wsid' ORDER BY (`$table_name3`) DESC LIMIT 0,1";
							break;
						case 'ESD程式':
							$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name2`='$webdl_wsid' ORDER BY (`$table_name3`) DESC LIMIT 0,1";
							break;
						default:
							echo "<script>alert('查无此程式下载的选项!!');history.go(-1);</script>";
							exit();
							break;
					}
					
					$result = mysqli_query($conn,$sql_cmd);	// 透过 execute_query 对象实例完成查询
					if ( !$result ) { 
						mysqli_close($conn);				// 关闭资料库
						echo "<script>alert('MySql资料库搜寻失败, 请跟开发人员查明原因...');history.go(-1);</script>";
						exit();
					}
					
					$rows = mysqli_num_rows($result);		// 返回结果集中栏位的数量。
					if ( $rows == 0 ) { 
						$sop_rows_fail = 1;
					} else {
						if ( $prg_rows_fail == 1 ) {
							echo '<br><font size="4" color="#000000"><b>1.SOP(制程规范):</b></font>';
						} else {
							echo '<br><font size="4" color="#000000"><b>2.SOP(制程规范):</b></font>';
						}
						echo '<table border="2" width="75%">
								<tr bgcolor="#70DB93">
									<th>No.</th>
									<th>工單</th>
									<th>Serial Number</th>
									<th>Part Number</th>
									<th>WSID</th>
									<th>TPVER</th>
									<th>线别</th>
									<th>制程别</th>
									<th>Create Date/Time</th>
									<th>Create By</th>
									<th>Description(位址/ChkSum)</th>
									<th>Download</th>
								</tr>';
						$show_num = 0;
						while ( $getArray = mysqli_fetch_array($result,MYSQLI_ASSOC) ) {
							// To loading all datas from MySQL ...
							$show_num++;
							$put_pn          = $getArray['webdl_pn'];
							$put_wsid        = $getArray['webdl_wsid'];
							$put_tpver       = $getArray['webdl_tpver'];
							$put_work_order  = $getArray['webdl_work_order'];
							$put_line        = $getArray['webdl_line'];
							$put_panel       = $getArray['webdl_panel'];
							$put_create_date = $getArray['webdl_create_date'];
							$put_creator     = $getArray['webdl_creator'];
							$put_description = $getArray['webdl_description'];
							$put_ftpip       = $getArray['webdl_ftpip'];
							$put_count       = $getArray['webdl_count'];
							// $download_ftpip  = "webdl_download_count.php?pn=$put_pn&wsid=$put_wsid&tpver=$put_tpver&work_order=$put_work_order&line=$put_line&panel=$put_panel&ftpip=$put_ftpip&webdl_count=$put_count";
							$download_ftpip  = "webdl_download_count.php?pn=$put_pn&wsid=$put_wsid&tpver=$put_tpver&work_order=$put_work_order&line=$put_line&panel=$put_panel&ftpip=$put_ftpip&webdl_count=$put_count&creator=$put_creator";
							echo "
								<tr>
									<th>$show_num</th>
									<th><font size='4' color='#0000FF'><b>$put_work_order</b></font></th>
									<th>$webdl_sn</th>
									<th>$put_pn</th>
									<th>$put_wsid</th>
									<th>$put_tpver</th>
									<th>$put_line</th>
									<th>$put_panel</th>
									<th>$put_create_date</th>
									<th>$put_creator</th>
									<th>$put_description</th>
									<th><a href=$download_ftpip>下 载</a></th>
								</tr>";
						}
						echo '</table><br>';
					}
					mysqli_free_result($result);			// 释放 MySQL $result
					mysqli_close($conn);					// 关闭资料库
				}
				
				if ( $get_prgm_select == 'SMT程式' && $prg_rows_fail == 1 ) {
					echo "<script>alert('查无此笔上传程式的资料, 请通知相关负责人建立...');history.go(-1);</script>";
				} elseif ( $prg_rows_fail == 1 && $sop_rows_fail == 1 ) {
					echo "<script>alert('查无此笔上传程式及SOP的资料, 请通知相关负责人建立...');history.go(-1);</script>";
				}
				exit();
			?>
		</center>
	</body>
</html>


